//
//  MainCenterTVPresenter.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/8.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit
import MJRefresh
enum MainVCOrderState {
    case new,my,finish
}

class MainCenterTVPresenter: BaseTVPresenter,orderCellClickDelegate {
    
    var newModels = [Any]()
    var myModels = [Any]()
    var finishModels = [Any]()

    var newPage:UInt = 1
    var myPage:UInt = 1
    var finishPage:UInt = 1
    
    var startDate:String = String.timeStampToString(timeS: Date.init(timeIntervalSinceNow: -(24*60*60*7)).timeIntervalSince1970 * 1000, dateStyle: "yyyy-MM-dd")

    var endDate:String = String.getNow()

   
    
    var orderState:MainVCOrderState = .new
        {
        didSet{
            
            let VC = self.vc as! MainVC
            var offset:Double = 0
            switch orderState {
            case .new:
                self.getNewOrder()
                VC.navView._title = "待接订单"
            case .my:
                self.getMyOrder()
                VC.navView._title = "我的订单"

            case .finish:
                self.getFinishOrder()
                //
                offset = 60
                VC.navView._title = "已完成订单"
            }
            
            tableView.snp.updateConstraints({ (make) in
                make.top.equalTo(VC.navView.snp.bottom).offset(offset)
            })
            VC.status = false
        }
    }
    
    
    override func setupTableView() {
        self.tableView.register(UINib.init(nibName: "OrderCell", bundle: nil), forCellReuseIdentifier: "OrderCell")
//        self.isLoadMore = true
//        self.isRefresh = true
        super.setupTableView()

    }
    
    
    
    override func refresh() {
        switch orderState {
        case .new:
            newPage = 1
            self.getNewOrder()
        case .my:
            myPage = 1
            self.getMyOrder()
            
        case .finish:
            finishPage = 1;
            self.getFinishOrder()
        }
    }
    
    override func loadMore() {
        switch orderState {
        case .new:
            newPage += 1
            self.getNewOrder(isRefresh: false)
        case .my:
            myPage += 1
            self.getMyOrder(isRefresh: false)
            
        case .finish:
            finishPage += 1;
            self.getFinishOrder(isRefresh: false)
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.contents?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OrderCell") as! OrderCell
        cell.setInterFace(WithModel: self.contents![indexPath.row] as! OrderModel)
        cell.thisDelegate = self
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.contents![indexPath.row] as! OrderModel
        let vc = OrderDetailVC()
        vc.orderID = model.orderID
        self.vc.present(vc, animated: true, completion: nil)
    }
    
    func handleOrder(WithDotType dotType:String, orderID:String)
    {
        NetWorkingManager.handleOrder(WithID: orderID, dotType: dotType,hintStr:"处理中", block: {(state, data) in
            switch state
            {
            case .Faild(let msg):
                self.vc.view.wkMakeToastToCenter(message: msg)
            case .Success:
                print("成功")
                //刷新
                self.refresh()
            }
        })

    }
    func getNewOrder(isRefresh:Bool = true)
    {
        NetWorkingManager.getNewOrder(page:UInt(newPage), hintStr: "获取数据中", block: {(state, data) in
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()

            switch state
            {
            case .Faild(let msg):
                self.vc.view.wkMakeToastToCenter(message: msg)
            case .Success:
                
                if let arr = data!.array
                {
                    if isRefresh
                    {
                        self.newModels.removeAll()
                    }
                    else
                    {
                        //如果没有数据 则页数要返回
                        if arr.count <= 0
                        {
                            self.newPage -= 1
                        }
                    }
                    for dictJson in arr
                    {
                        if let dict = dictJson.dictionary
                        {
                            let model = OrderModel.init(WithDict: dict)
                            self.newModels.append(model)
                        }
                    }
                    self.contents = self.newModels
//                    self.contents?.sort(by: { (model1, model2) -> Bool in
//                            let one = String.stringToTimeStamp(stringTime: (model1 as! OrderModel).orderAddTime)
//                            let two = String.stringToTimeStamp(stringTime: (model2 as! OrderModel).orderAddTime)
//                            return one < two
//                    })
                    
                }
            }
        })
    }
    
    
    func getFinishOrder(isRefresh:Bool = true)
    {
        NetWorkingManager.getFinishOrder(startDate:startDate, endDate: endDate,page:finishPage, hintStr: "获取数据中", block: {(state, data) in
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()

            switch state
            {
            case .Faild(let msg):
                self.vc.view.wkMakeToastToCenter(message: msg)
            case .Success:
                if let arr = data!.array
                {
                    if isRefresh
                    {
                        self.finishModels.removeAll()
                    }
                    else
                    {
                        //如果没有数据 则页数要返回
                        if arr.count <= 0
                        {
                            self.finishPage -= 1
                        }
                    }
                    for dictJson in arr
                    {
                        if let dict = dictJson.dictionary
                        {
                            let model = OrderModel.init(WithDict: dict)
                            self.finishModels.append(model)
                        }
                    }
                    self.contents = self.finishModels
//                    self.contents?.sort(by: { (model1, model2) -> Bool in
//                        let one = String.stringToTimeStamp(stringTime: (model1 as! OrderModel).orderAddTime)
//                        let two = String.stringToTimeStamp(stringTime: (model2 as! OrderModel).orderAddTime)
//                        return one < two
//                    })
                }
            }
        })
    }
    
    func getMyOrder(isRefresh:Bool = true)
    {
        NetWorkingManager.getMyOrder(page:myPage,hintStr: "获取数据中", block: {(state, data) in
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()

            switch state
            {
            case .Faild(let msg):
                self.vc.view.wkMakeToastToCenter(message: msg)
            case .Success:
                print(data)
                if let arr = data!.array
                {
                    if isRefresh
                    {
                        self.myModels.removeAll()
                    }
                    else
                    {
                        //如果没有数据 则页数要返回
                        if arr.count <= 0
                        {
                            self.myPage -= 1
                        }
                    }
                    for dictJson in arr
                    {
                        if let dict = dictJson.dictionary
                        {
                            let model = OrderModel.init(WithDict: dict)
                            self.myModels.append(model)
                        }
                    }
                    self.contents = self.myModels
//                    self.contents?.sort(by: { (model1, model2) -> Bool in
//                        let one = String.stringToTimeStamp(stringTime: (model1 as! OrderModel).orderAddTime)
//                        let two = String.stringToTimeStamp(stringTime: (model2 as! OrderModel).orderAddTime)
//                        return one < two
//                    })
                }
            }
        })
    }
}
